<route lang="json">
{
    "name": "addressManage",
    "meta": {
        "title": "控制台-地址管理",
        "requiresAuth": true
    }
}
</route>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { computed, onMounted, reactive, ref } from 'vue'
import { getAddressList } from '~/api/address'

// 定义地址接口类型
interface Address {
  addressid: string
  userid?: string
  name: string
  tel: string
  province: string
  city: string
  county?: string
  addressDetail: string
  isDefault: boolean
}

// 地址列表数据
const addressList = ref<Address[]>([])
const loading = ref(false)

// 表格分页
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0,
})

// 获取地址列表
async function fetchAddressList() {
  loading.value = true
  try {
    const res = await getAddressList()
    console.warn('API返回的地址数据:', res)

    // 处理API返回的数据
    if (res && Array.isArray(res)) {
      addressList.value = res
      pagination.total = res.length
      console.warn('成功获取地址列表数据，共', addressList.value.length, '条记录')
    }
    else if (res && typeof res === 'object' && Array.isArray(res.data)) {
      addressList.value = res.data
      pagination.total = res.data.length
      console.warn('成功获取地址列表数据，共', addressList.value.length, '条记录')
    }
    else {
      addressList.value = []
      pagination.total = 0
      console.warn('API返回的数据格式不符合预期:', res)
    }
  }
  catch (error) {
    console.error('获取地址列表失败:', error)
    ElMessage.error('获取地址列表失败')
    addressList.value = []
    pagination.total = 0
  }
  finally {
    loading.value = false
  }
}

// 处理分页变化
function handleCurrentChange(val: number) {
  pagination.currentPage = val
}

function handleSizeChange(val: number) {
  pagination.pageSize = val
  pagination.currentPage = 1
}

// 计算当前页的数据
const currentPageData = computed(() => {
  const start = (pagination.currentPage - 1) * pagination.pageSize
  const end = start + pagination.pageSize
  return addressList.value.slice(start, end)
})

// 页面加载时获取地址列表
onMounted(() => {
  fetchAddressList()
})

const waterMarkOptions = ref({
  text: '机密文件',
  fontSize: 24,
  density: 'normal',
})
</script>

<template>
  <div v-watermark="waterMarkOptions" class="address-manage-container">
    <div class="address-header">
      <h2 class="address-title">
        地址管理
      </h2>
    </div>

    <!-- 地址列表 -->
    <el-card shadow="hover" class="address-card">
      <el-table v-loading="loading" :data="currentPageData" stripe border style="width: 100%">
        <!-- 序号列 -->
        <el-table-column type="index" label="序号" width="60" />

        <!-- ID列 -->
        <el-table-column prop="addressid" label="地址ID" min-width="200" show-overflow-tooltip />

        <!-- 用户ID列 -->
        <el-table-column prop="userid" label="用户ID" min-width="200" show-overflow-tooltip>
          <template #default="scope">
            {{ scope.row.userid || '-' }}
          </template>
        </el-table-column>

        <!-- 收件人列 -->
        <el-table-column prop="name" label="收件人" min-width="100">
          <template #default="scope">
            {{ scope.row.name || '-' }}
          </template>
        </el-table-column>

        <!-- 电话列 -->
        <el-table-column prop="tel" label="联系电话" min-width="120">
          <template #default="scope">
            {{ scope.row.tel || '-' }}
          </template>
        </el-table-column>

        <!-- 省份列 -->
        <el-table-column prop="province" label="省份" min-width="100">
          <template #default="scope">
            {{ scope.row.province || '-' }}
          </template>
        </el-table-column>

        <!-- 城市列 -->
        <el-table-column prop="city" label="城市" min-width="100">
          <template #default="scope">
            {{ scope.row.city || '-' }}
          </template>
        </el-table-column>

        <!-- 区县列 -->
        <el-table-column prop="county" label="区县" min-width="100">
          <template #default="scope">
            {{ scope.row.county || '-' }}
          </template>
        </el-table-column>

        <!-- 详细地址列 -->
        <el-table-column prop="addressDetail" label="详细地址" min-width="150" show-overflow-tooltip>
          <template #default="scope">
            {{ scope.row.addressDetail || '-' }}
          </template>
        </el-table-column>

        <!-- 默认地址列 -->
        <el-table-column prop="isDefault" label="是否默认" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.isDefault ? 'success' : 'info'">
              {{ scope.row.isDefault ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage" v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
          @size-change="handleSizeChange" @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
.address-manage-container {
  padding: 20px;

  .address-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    .address-title {
      font-size: 20px;
      font-weight: 600;
      margin: 0;
    }
  }

  .address-card {
    margin-bottom: 20px;
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
